window.yjzyIsInit = false;
window.imageYlObj = {};
let yjzyAll = {
    ry:[],
    cl:[],
    wz:[],
};
$(function () {
    //仓库
    $("#kc_id").on('click', function () {
        //清除聚合点
        clearCheckBox();

        setYjstatus();
        $.ajax({
            url: baseUrl + "/yjwzgl/main/emergencymaterialvisualization/getinventorystatistics",
            type: "get",
            dataType: "json",
            success: function (data) {
                map.clearOverlays();
                let allPointList = [];
                for (let n = 0; n < data.length; n++) {
                    allPointList.push({lng:data[n].lon,lat:data[n].lat});
                    var point = new BMap.Point(data[n].lon, data[n].lat);
                    var myIcon = new BMap.Icon("img/y-j.png", new BMap.Size(24, 24));
                    var marker = new BMap.Marker(point, {
                        icon: myIcon
                    }); // 创建标注
                    marker.addEventListener("click", function(e){
						console.log(data[n].warehouseName)
                        dt_point(e,data[n],data[n].warehouseName);
                    });
                    map.addOverlay(marker); // 将标注添加到地图中
                }
                setCenterAndZoom(allPointList);
            }
        });
    })
    //应急资源
    $('.yj-con .yj-col').click(function(){
        if(window.yjzyIsInit === false){
            map.clearOverlays();
            window.yjzyIsInit = true;
        }
        let dataType = $(this).attr('data-type');
        if($(this).attr('is-select') == 1){
            $(this).removeClass('yj-active');
            $(this).attr('is-select','0');
            if(dataType == '1'){
                yjzyAll['ry'] = [];
                deletePointForTitle('yj-ry');
            }else if(dataType == '2'){
                yjzyAll['cl'] = [];
                deletePointForTitle('yj-cl');
            }else if(dataType == '3'){
                yjzyAll['wz'] = [];
                deletePointForTitle('yj-wz');
            }
            setYjAllPoint();
        }else{
            $(this).addClass('yj-active');
            $(this).attr('is-select','1');
            //清除聚合点
            clearCheckBox();

            if($(this).attr('data-type') == '1'){
                $.ajax({
                    url: baseUrl + "yjwzgl/main/emergencymaterialvisualization/getpeoplestatistics",
                    type: "get",
                    dataType: "json",
                    success: function (data) {
                        let allPointList = [];
                        for (let n = 0; n < data.length; n++) {
                            allPointList.push({lng:data[n].lon,lat:data[n].lat});
                            var point = new BMap.Point(data[n].lon, data[n].lat);
                            var myIcon = new BMap.Icon("img/应急人_u555.svg", new BMap.Size(24, 24));
                            var marker = new BMap.Marker(point, {
                                icon: myIcon,
                                title:'yj-ry'
                            }); // 创建标注
                            marker.addEventListener("click", ry_point.bind(this, data[n]));
                            map.addOverlay(marker); // 将标注添加到地图中
                        }
                        yjzyAll['ry'] = allPointList;

                        setYjAllPoint();
                    }
                });
            }else if($(this).attr('data-type') == '2'){
                $.ajax({
                    url: baseUrl + "yjwzgl/main/emergencymaterialvisualization/getvehiclestatistics",
                    type: "get",
                    dataType: "json",
                    success: function (data) {
                        
                        let allPointList = [];
                        for (let n = 0; n < data.length; n++) {
                            allPointList.push({lng:data[n].lon,lat:data[n].lat});
                            var point = new BMap.Point(data[n].lon, data[n].lat);
                            var myIcon = new BMap.Icon("img/应急车_u558.svg", new BMap.Size(24, 24));
                            var marker = new BMap.Marker(point, {
                                icon: myIcon,
                                title:'yj-cl'
                            }); // 创建标注
                            marker.addEventListener("click", cl_point.bind(this, data[n]));
                            map.addOverlay(marker); // 将标注添加到地图中
                        }
                        yjzyAll['cl'] = allPointList;
                        setYjAllPoint();
                    }
                });
            }else if($(this).attr('data-type') == '3'){
                $.ajax({
                    url: baseUrl + "yjwzgl/main/emergencymaterialvisualization/getmaterialinformation?fid=",
                    type: "get",
                    dataType: "json",
                    success: function (data) {

                        let allPointList = [];
                        for (let n = 0; n < data.length; n++) {
                            allPointList.push({lng:data[n].lon,lat:data[n].lat});
                            var point = new BMap.Point(data[n].lon, data[n].lat);
                            var myIcon = new BMap.Icon("img/应急物_u560.svg", new BMap.Size(24, 24));
                            var marker = new BMap.Marker(point, {
                                icon: myIcon,
                                title:'yj-wz'
                            }); // 创建标注
                            marker.addEventListener("click", wz_point.bind(this, data[n]));
                            map.addOverlay(marker); // 将标注添加到地图中
                        }
                        yjzyAll['wz'] = allPointList;
                        setYjAllPoint();
                    }
                });
            }
        }
        
    });
    $("#yjsj_id").click(function () {
        var startTime = JSON.parse(localStorage.getItem("startTime"));
        var endTime = JSON.parse(localStorage.getItem("endTime"));

        setYjstatus();
        //清除聚合点
        clearCheckBox();
        $.ajax({
            url: baseUrl + "yjwzgl/main/emergencymaterialvisualization/getemergencyall?startTime="+startTime+'&endTime='+endTime,
            type: "get",
            dataType: "json",
            success: function (data) {
                map.clearOverlays();

                let allPointList = [];
                for (let n = 0; n < data.length; n++) {
                    allPointList.push({lng:data[n].lon,lat:data[n].lat});
                    var point = new BMap.Point(data[n].lon, data[n].lat);
                    var myIcon = new BMap.Icon("img/y-s.png", new BMap.Size(24, 24));
                    var marker = new BMap.Marker(point, {
                        icon: myIcon
                    }); // 创建标注
                    marker.addEventListener("click", function(e){
                        yjsj_point(e, data[n]);
                    });
                    map.addOverlay(marker); // 将标注添加到地图中
                }
                setCenterAndZoom(allPointList);
            }
        });
    })
    $('.detailModel .model-close').click(function () {
        $('.detailModel').hide();
        $('.layui-layer-shade').hide();
        $('.detailModel table').remove();
    });
        //应急事件 标签点击
    $('.detailModel').on('click','.model-menu a',function () {
        $(this).addClass("active").siblings("a").removeClass("active");
        let conId = $(this).attr('conid');
        $('#' + conId).show().siblings().hide();
    });
        //图纸预览
    $('.detailModel').on('click', '.tz-yl', function () {
        let id = $(this).attr('data-id');
        window.imageYlObj[id].show();
    });
})
function deletePointForTitle(title){
    var allOverlay = map.getOverlays();
    for (var i = 0; i < allOverlay.length; i++){
        if(allOverlay[i].getTitle() == title){
            map.removeOverlay(allOverlay[i]);
        }
    }
}
function setYjstatus(){
    window.yjzyIsInit = false;
    $('.yj-con .yj-col').removeClass('yj-active');
    $('.yj-con .yj-col').attr('is-select','0');
}
function setYjAllPoint(){
    let allPointList = [];
    $.each(yjzyAll['ry'],function(i,item){
        allPointList.push(item);
    });
    $.each(yjzyAll['cl'],function(i,item){
        allPointList.push(item);
    });
    $.each(yjzyAll['wz'],function(i,item){
        allPointList.push(item);
    });
    if(allPointList.length>0){
        setCenterAndZoom(allPointList);
    }
}
function dt_point(e, param,name) {
    //根据id获取仓库库存
    $.ajax({
        url: baseUrl + "yjwzgl/main/emergencymaterialvisualization/getinventoryaccordingwarehouse?WarehouseCode=" + param.warehouseCode,
        type: "get",
        dataType: "json",
        success: function (data) {
            $('.pointDisNCz') && $('.pointDisNCz').remove();
            $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
            $('.detailModel .wz-con').length && $('.detailModel .wz-con').remove();
            let dataJson = data;
            let trDom = '';
            $.each(data,function(i,item){
				console.log(item)
                trDom+=`<tr><th>${i+1}</th><th>${item.materialcode}</th><th>${item.materialname}</th><th>${item.classname}</th><th>${item.spec == undefined ? '--': item.spec}</th><th>${item.units}</th><th>${item.number}</th></tr>`;
            });
            $('.detailModel').append('<div class="wz-con" style="padding:20px;color:rgba(7, 251, 246, 1);font-size:0.17rem">仓库名称：'+name+'<table class="table" style="margin-bottom: 0;">' +
            `<tr><th>序号</th><th>物资编码</th><th>物资名称</th><th>物资类型</th><th>规格型号</th><th>单位</th><th>数量</th></tr>`+trDom+
            '</table></div>');
            
            $('.detailModel').show();
            $('.layui-layer-shade').show();
            return;
            // $('.pointDisNCz') && $('.pointDisNCz').remove();
            // $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
            // let dataJson = data;
            // $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
            // $('.disNCz').hide();
            // if ($(e.target.Ac).find('.pointDisNCz') && $(e.target.Ac).find('.pointDisNCz').css('display') == 'block') {
            //     $(e.target.Ac).find('.pointDisNCz').remove();
            // } else {
            //     $(e.target.Ac).find('.pointDisNCz') && $(e.target.Ac).find('.pointDisNCz').remove();
            //     let trDom = '';
            //     $.each(data,function(i,item){
            //         trDom+=`<tr><th>${i+1}</th><th>${item.materialcode}</th><th>${item.materialname}</th><th>${item.classname}</th><th>${item.spec}</th><th>${item.units}</th><th>${item.number}</th></tr>`;
            //     });
            //     var htmlStr =
            //     '<div class="pointDisNCz" style="position:absolute;padding:10px; left: -160px; width:688px; bottom:20px;z-index: 9999;background-color:#1D2127; border: 1px solid #0eb1fd;color:#fff; font-size:16px">' +
            //     '<div class="yad-list"><i id="pointGbclod"><img src="img/icon.png" alt="" /></i>' +
            //     '<table class="table" style="margin-bottom: 0;">' +
            //     `<tr><th>序号</th><th>物资编码</th><th>物资名称</th><th>物资类型</th><th>规格型号</th><th>单位</th><th>数量</th></tr>`+trDom+
            //     '</table></div></div>';
            //     $(e.target.Ac).append(htmlStr);
            //     $(e.target.Ac).find('#pointGbclod').click(function () {
            //         $(e.target.Ac).find('.pointDisNCz').remove();
            //     })

            // }
            // alert(JSON.stringify(data));
        }
    });
}

function ry_point(e, data) {
    $('.pointDisNCz') && $('.pointDisNCz').remove();
    $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
    $(data.currentTarget.Ac).append(
        '<div class="pointDisNCz" style="position:absolute;padding:10px; left: -160px; width:688px; bottom:20px;z-index: 9999;background-color:#1D2127; border: 1px solid #0eb1fd;color:#fff; font-size:16px">' +
        '<div class="yad-list">\n' +
        '<i id="pointGbclod"><img src="img/icon.png" alt="" /></i>' +
        '<table class="table" style="margin-bottom: 0;">' +
        '<tr>' +
        '<th>姓名</th>' +
        '<td>' + e.name + '</td>' +
        '<th>单位</th>' +
        '<td>' + e.units + '</td>' +
        '</tr>' +
        '<tr>' +
        '<th>部门</th>' +
        '<td>' + e.dept + '</td>' +
        '<th>职务</th>' +
        '<td>' + e.post + '</td>' +
        '</tr>' +
        '<tr>' +
        '<th>手机号</th>' +
        '<td>' + (e.dutytelephone === undefined ? "" : e.dutytelephone) + '</td>' +
        '<th>值班电话</th>' +
        '<td>' + (e.dutytelephone === undefined ? "" : e.dutytelephone) + '</td>' +
        '</tr>' +
        '</table>' +
        '</div></div>'
    );
    $('#pointGbclod').click(function () {
        $(data.currentTarget.Ac).find('.pointDisNCz').remove();
    })
}

function cl_point(e, data) {
    $('.pointDisNCz') && $('.pointDisNCz').remove();
    $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
    $(data.currentTarget.Ac).append(
        '<div class="pointDisNCz" style="position:absolute;padding:10px; left: -160px; width:688px; bottom:20px;z-index: 9999;background-color:#1D2127; border: 1px solid #0eb1fd;color:#fff; font-size:16px">' +
        '<div class="yad-list">\n' +
        '<i id="pointGbclod"><img src="img/icon.png" alt="" /></i>' +
        '<table class="table" style="margin-bottom: 0;">' +
        '<tr>' +
        '<th>车牌号</th>' +
        '<td>' + e.vehicleno + '</td>' +
        '<th>SIM卡号</th>' +
        '<td>' + (e.simid || '--') + '</td>' +
        '</tr>' +
        '<tr>' +
        '<th>单位</th>' +
        '<td>' + e.units + '</td>' +
        '<th>视频url</th>' +
        '<td>' + (e.videourl || '--') + '</td>' +
        '</tr>' +
        '</table>' +
        '</div></div>'
    );
    $('#pointGbclod').click(function () {
        $(data.currentTarget.Ac).find('.pointDisNCz').remove();
    })
}

function wz_point(e, data) {
    $('.pointDisNCz') && $('.pointDisNCz').remove();
    $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
    $(data.currentTarget.Ac).append(
        '<div class="pointDisNCz" style="position:absolute;padding:10px; left: -160px; width:688px; bottom:20px;z-index: 9999;background-color:#1D2127; border: 1px solid #0eb1fd;color:#fff; font-size:16px">' +
        '<div class="yad-list">\n' +
        '<i id="pointGbclod"><img src="img/icon.png" alt="" /></i>' +
        '<table class="table" style="margin-bottom: 0;">' +
        '<tr>' +
        '<th>存放地点</th>' +
        '<td>' + e.position + '</td>' +
        '<th>单位</th>' +
        '<td>' + e.unitid + '</td>' +
        '</tr>' +
        '<tr>' +
        '<th>联系人</th>' +
        '<td>' + e.contactid + '</td>' +
        '<th>联系电话</th>' +
        '<td>' + e.contacttel + '</td>' +
        '</tr>' +
        '<tr>' +
        '<th>经度</th>' +
        '<td>' + e.lon + '</td>' +
        '<th>纬度</th>' +
        '<td>' + e.lat + '</td>' +
        '</tr>' +
        '<tr>' +
        '<th>物资内容</th>' +
        '<td colspan="4">' + e.content + '</td>' +
        '</tr>' +
        '</tr>' +
        '<tr>' +
        '<th>备注</th>' +
        '<td colspan="4">' + (e.remark === undefined ? "" : e.remark) + '</td>' +
        '</tr>' +
        '</table>' +
        '</div></div>'
    );
    $('#pointGbclod').click(function () {
        $(data.currentTarget.Ac).find('.pointDisNCz').remove();
    })
}

function yjsj_point(e, param) {
    // debugger
    $.ajax({
        url: baseUrl + "yjwzgl/main/emergencymaterialvisualization/getemergencybyid?fid=" + param.fid,
        type: "get",
        dataType: "json",
        success: function (data) {
            $('.pointDisNCz') && $('.pointDisNCz').remove();
            $('.disNCz').find('.yad-list') && $('.disNCz').find('.yad-list').remove();
            $('.detailModel .wz-con').length && $('.detailModel .wz-con').remove();
            let dataJson = data;
            let img1Dom = '',img1List,imageNearYlDom = '';
            let img2Dom = '',img2List,imageFarYlDom = '';

            if(data.data.nearImg && data.data.nearImg.length>0){
                img1List = JSON.parse(data.data.nearImg);
                if(img1List.length>0){
                    imageNearYlDom += '<ul class="image-ylul" id="imageNearYl" style="display:none;">';
                    img1Dom+= '<span>近景照片</span>';
                    $.each(img1List,function(i,item){
                        imageNearYlDom +=`<li><img data-original="${yjzyImageBaseUrl+item.storeFileName}" src="${yjzyImageBaseUrl+item.storeFileName}"></li>`;
                        img1Dom+= `<img class="tz-yl" data-id="imageNearYl" src="${yjzyImageBaseUrl+item.storeFileName}" style="margin-left:10px;width:150px;cursor: pointer;" alt="点击查看大图" />`
                    });
                    imageNearYlDom += '</ul>';
                }
            }
            if(data.data.farImg && data.data.farImg.length>0){
                img2List = JSON.parse(data.data.farImg);
                if(img2List.length>0){
                    imageFarYlDom += '<ul class="image-ylul" id="imageFarYl" style="display:none;">';
                    img2Dom+= '<span style="margin-left:10px;">远景照片</span>';
                    $.each(img2List,function(i,item){
                        imageFarYlDom +=`<li><img data-original="${yjzyImageBaseUrl+item.storeFileName}" src="${yjzyImageBaseUrl+item.storeFileName}"></li>`;
                        img2Dom+= `<img class="tz-yl" data-id="imageFarYl" src="${yjzyImageBaseUrl+item.storeFileName}" style="margin-left:10px;width:150px;cursor: pointer;" alt="点击查看大图" />`
                    });
                    imageFarYlDom += '</ul>';
                }
            }
            let yjya = '',yjya2 = '',yjya3 = '',yjya4='';
            $.each(data.PlanRec,function(i,item){
                yjya += item.content;
            });
            $.each(data.Person,function(i,item){
                yjya2 += '&nbsp;&nbsp;&nbsp;&nbsp;'+item.name + '  ' + item.units + '  '+item.post+ '  '+item.phoneno+'</br>';
            });
            $.each(data.MaterialTransfer,function(i,item){
                yjya3 += '&nbsp;&nbsp;&nbsp;&nbsp;'+item.content + '  ' + item.unitid + '  '+item.position+ '  '+item.contactid+'  '+item.contacttel+'</br>';
            });
            $.each(data.Vehicleinfo,function(i,item){
                yjya4 += '&nbsp;&nbsp;&nbsp;&nbsp;'+item.vehicleno + '  ' + item.units+'</br>';
            });
            $('.detailModel').append(`<div class="wz-con" style="padding:20px;">
                <div class="sj-opt">
                    <div class="model-menu">
                        <a style="margin-left:0px;" href="javascript:void(0)" conid="menu1" class="active">事件基本信息</a>
                        <a href="javascript:void(0)" conid="menu2">应急方案</a>
                    </div>
                </div>
                <div class="yjzy-content" style="overflow-y: auto;">
                    <div id="menu1" class="sj-jbxi" style=""style="padding:20px;">
                    <table class="table" style="margin-bottom: 0;">
                    <tr><th>行政区域</th><td>${data.data.location || '--'}</td><th>设施类型</th><td>${data.data.kinds || '--'}</td></tr>
                    <tr><th>设施名称</th><td>${data.data.facilityname || '--'}</td><th>详细类型</th><td>${data.data.devicetype || '--'}</td></tr>
                    <tr><th>行驶方向</th><td>${data.data.directionid || '--'}</td><th>事件类型</th><td>${data.data.eventtype || '--'}</td></tr>
                    <tr><th>事件等级</th><td>${data.data.eventgrade || '--'}</td><th>业务来源</th><td>${data.data.sourseid || '--'}</td></tr>
                    <tr><th>经度</th><td>${data.data.lon || '--'}</td><th>纬度</th><td>${data.data.lat || '--'}</td></tr>
                    <tr><th>现场负责人</th><td>${data.data.chargepersonid || '--'}</td><th>联系电话</th><td>${data.data.contacttel || '--'}</td></tr>
                    <tr><th>发展趋势</th><td>${data.data.developmenttrend || '--'}</td><th>事件发生范围</th><td>${data.data.eventscope || '--'}</td></tr>
                    <tr><th>原因分析</th><td>${data.data.causeanalysis || '--'}</td><th>交通情况</th><td>${data.data.trafficsituation || '--'}</td></tr>
                    <tr><th>交通情况补充</th><td>${data.data.trafficsituationext || '--'}</td><th>挖掘范围</th><td>${data.data.excavationscope || '--'}</td></tr>
                    <tr><th>预计完工时间</th><td>${data.data.completedate || '--'}</td><th>人员伤</th><td>${data.data.personinjury}</td></tr>
                    <tr><th>人员亡</th><td colspan="3">${data.data.persondie}</td></tr>
                    <tr><th>现场描述</th><td colspan="3">${data.data.sitedescripte || '--'}</td></tr>
                    <tr><th>具体位置</th><td colspan="3">${data.data.specificlocation || '--'}</td></tr>
                    <tr><th>已采取措施</th><td colspan="3">${data.data.measurestaken || '--'}</td></tr>
                    <tr><th>备注</th><td colspan="3">${data.data.remark || '--'}</td></tr>
                    <tr><th>上传照片</th><td colspan="3"><div style="display:flex;">${img1Dom+img2Dom}${imageNearYlDom}${imageFarYlDom}</div></td></tr>
                    </table>
                    </div>
                    <div id="menu2" class="sj-fa" style="display:none;">
                        <div class="sj-fa-val" style="font-size:16px;color:#ffffff;line-height: 1.7;">
                        一、基本情况<br/>
                        &nbsp;&nbsp;&nbsp;&nbsp${dateFormat('YYYY-mm-dd',new Date(data.data.reportingdate))}，
                        在${data.data.facilityname}发生了${data.data.devicetype}事件，现场情况为${data.data.sitedescripte}，
                        目前交通情况${data.data.trafficsituation}，事件的影响范围${data.data.eventscope}，现场的负责人员${data.data.chargepersonid}，预计事件的处理时间会持续到${data.data.completedate}。
                        <br/>
                        二、应急预案<br/>
                        &nbsp;&nbsp;&nbsp;&nbsp;${yjya}
                        <br/>
                        三、处理方式<br/>
                        &nbsp;&nbsp;&nbsp;&nbsp;${data.data.handlingmethod}
                        <br/>
                        四、负责人员<br/>
                        ${yjya2}
                        五、物资调取<br/>
                        ${yjya3}
                        六、车辆调取<br/>
                        ${yjya4}
                        七、其他说明<br/>
                        &nbsp;&nbsp;&nbsp;&nbsp;${data.data.explain}
                        </div>
                    </div>
                </div>
            </div>`);
            $('.detailModel').show();
            $('.yjzy-content').height(($('.detailModel').height()-100)+'px');
            $('.layui-layer-shade').show();
            if(imageNearYlDom){
                window.imageYlObj['imageNearYl'] = new Viewer(document.getElementById(
                    'imageNearYl'), {
                        url: 'data-original',
                        toolbar: false
                    });
            }
            if(imageFarYlDom){
                window.imageYlObj['imageFarYl'] = new Viewer(document.getElementById(
                    'imageFarYl'), {
                        url: 'data-original',
                        toolbar: false
                    });
            }
        }
    });
}